package com.jcf.springboot.controller.business;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jcf.springboot.common.R;
import com.jcf.springboot.entity.business.Actor;
import com.jcf.springboot.entity.business.FilmActor;
import com.jcf.springboot.entity.business.vo.ActorDef;
import com.jcf.springboot.service.ActorService;
import com.jcf.springboot.service.FilmActorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjj
 * @since 2021-03-25
 */
@RestController
@CrossOrigin
@RequestMapping("/actor")
@Slf4j
public class ActorController {


    @Autowired
    private ActorService actorService;

    @Autowired
    private FilmActorService filmActorService;


    @GetMapping("getActorList")
    public R getActorList(){

        List<ActorDef> list = actorService.getActorList();
        if(list != null){
            return R.ok().data(list).message("获取演员列表成功");
        }
        else{
            return R.error().message("获取演员列表失败");
        }
    }

    @GetMapping("getActorById")
    public R getActorById(String id){
        ActorDef actorDef = new ActorDef();
        Actor actor = actorService.getById(id);
        QueryWrapper<FilmActor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("actor_id", id);
        FilmActor filmActor = filmActorService.getOne(queryWrapper);
        BeanUtils.copyProperties(actor,actorDef);
        BeanUtils.copyProperties(filmActor,actorDef);
        if(actorDef != null){
            return R.ok().data(actorDef).message("获取演员成功");
        }
        else{
            return R.error().message("获取演员失败");
        }
    }

    @GetMapping("getActorByFilmId")
    public R getDirectorByFilmId(String id){

        List<ActorDef> actorDefs = actorService.getActorByFilmId(id);
        if(actorDefs != null){
            return R.ok().data(actorDefs).message("获取演员成功");
        }
        else{
            return R.error().message("获取演员失败");
        }
    }

    @PutMapping("editActor")
    public R editActor(@RequestBody ActorDef actorDef){
        Actor actor = new Actor();
        FilmActor filmActor = new FilmActor();
        BeanUtils.copyProperties(actorDef, actor);
        boolean b = actorService.updateById(actor);
        BeanUtils.copyProperties(actorDef, filmActor);
        UpdateWrapper<FilmActor> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("actor_id", filmActor.getActorId());
        boolean b1 = filmActorService.update(filmActor,updateWrapper);


        if(b){
            if(b1){
                return R.ok().message("修改演员信息成功");
            }
            else {
                return R.error().message("修改演员信息饰演角色失败");
            }

        }
        else{
            return R.error().message("修改演员信息失败");
        }
    }
}

